/*
 * CGameStateMachine.cpp
 *
 *  Created on: Aug 31, 2011
 *      Author: welcome
 */
#include "CGameBase.h"
#include "CGameStateMachine.h"

namespace GGEngine {

CGameStateMachine::CGameStateMachine(): currentState(SM_STATE_UNKNOWN), nextState(SM_STATE_UNKNOWN), scheduledTime(-1) {
	stateMatrix = KD_NULL;
}

CGameStateMachine::~CGameStateMachine() {

}

void CGameStateMachine::ScheduleStateChange(KDint state, KDint delay)
{
    nextState = state;
    if (nextState == SM_STATE_UNKNOWN)
        throw std::runtime_error("StateMachine: scheduled state cannot be SM_STATE_UNKNOWN");
    scheduledTime = CGameBase::GetFrametime() + delay;
}

bool CGameStateMachine::Tick()
{
	bool res = false;

    if (CGameBase::GetFrametime() >= scheduledTime)
    {
        // switch state only if not already switched earlier
		if (currentState != nextState)
		{
			res = true;

			currentState = nextState;
		}
    }

	return res;
}

} /* namespace GGE */
